package com.mashibing.net.nettychat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.GlobalEventExecutor;

/**
 * @description:
 * @author: 余希瑶
 * @date: 2021年06月25日 10:24
 * @version:1.0
 */
public class Server {

    public  ChannelGroup clients = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    public  void serverStart(){

        //负责接客
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        //负责服务
        NioEventLoopGroup workerGroup = new NioEventLoopGroup(4);


        try {
            //Server启动辅助类
            ServerBootstrap b = new ServerBootstrap();
            //异步全双工
            b.group(bossGroup, workerGroup);

            b.channel(NioServerSocketChannel.class);

            //netty 帮我们内部处理了accept的过程
            b.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel socketChannel) throws Exception {
                    System.out.println("a client connected !");
                    socketChannel.pipeline().addLast(new ServerChildHandler());
                }
            });
            ChannelFuture future = b.bind(8888).sync();
            ServerFrame.INSTANCE.updateServerMsg("Server stared!");
            future.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }

     class ServerChildHandler extends ChannelInboundHandlerAdapter {

        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            clients.add(ctx.channel());
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//            ByteBuf buf = (ByteBuf) msg;
//            System.out.println(buf.toString());
//            ctx.writeAndFlush(msg);
            //buf.release();
            ByteBuf buf = null;
                buf =(ByteBuf)msg;
                byte [] bytes =new byte[buf.readableBytes()];
                buf.getBytes(buf.readerIndex(),bytes);
                String str = new String(bytes);
                if(str.equals("--byte--")){
                    System.out.println("client ready to quit.");
                    clients.remove(ctx.channel());
                    ctx.close();

                    System.out.println(clients.size());
                }else{
                    clients.writeAndFlush(buf);
                }
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            cause.printStackTrace();

            //client 下线了移除
            clients.remove(ctx.channel());
            ctx.close();
        }
    }
}

